package com.jinmdz.fmis.api.api.service;

import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.model.config.dictionary.DictionaryCode;
import com.jinmdz.fmis.api.wrapper.DictionaryWrapper;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.wrapper.ResultWrapper;
import com.jinmdz.fmis.dao.model.dictionary.DataDictionaryItem;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * DictionaryService类
 *
 * @author LiCongLu
 * @date 2020-01-13 11:38
 */
@Service("dictionaryService")
public class DictionaryService extends BaseService {

    @Resource
    private DictionaryWrapper dictionaryWrapper;

    @Resource
    private DictionaryCode dictionaryCode;

    /**
     * 查询字典数据
     *
     * @param data 字典类型数据
     * @return
     * @author LiCongLu
     * @date 2020-01-13 11:49
     */
    public BaseResult<HashMap<String, ArrayList<DataDictionaryItem>>> loadDictionary(ArrayList<String> data) {
        if (data.size() == 0) {
            return failure("没有请求任何字典类型");
        }
        HashMap<String, ArrayList<DataDictionaryItem>> dataMap = loadDictionaryMap(data);
        // 创建返回结果
        BaseResult<HashMap<String, ArrayList<DataDictionaryItem>>> result = ResultWrapper.getBaseResult();
        return success(result.setData(dataMap));
    }

    /**
     * 按照字典编码加载字典数据
     *
     * @param data 字典编码集合
     * @return
     * @author LiCongLu
     * @date 2020-02-16 11:03
     */
    public HashMap<String, ArrayList<DataDictionaryItem>> loadDictionaryMap(ArrayList<String> data) {
        // 遍历缓存数据
        HashMap<String, ArrayList<DataDictionaryItem>> dataMap = new HashMap<>(16);
        if (DataUtil.valid(data)) {
            HashMap<String, ArrayList<DataDictionaryItem>> cacheMap = dictionaryWrapper.mapDictionary();
            if (cacheMap != null && cacheMap.size() > 0) {
                for (String key : data) {
                    if (cacheMap.containsKey(key)) {
                        dataMap.put(key, cacheMap.get(key));
                    }
                }
            }
        }
        return dataMap;
    }
}
